package com.gzxw.mogublog.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.SysParams;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.SysParamsMapper;
import com.gzxw.mogublog.xo.service.SysParamsService;
import org.springframework.stereotype.Service;

@Service
public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParams> implements SysParamsService {

    // 根据key获取值
    @Override
    public String getSysParamsValueByKey(String paramsKey) {
        SysParams sysParams = this.getSysParamsByKey(paramsKey);
        // 如果数据库也不存在，将抛出异常【需要到找到 doc/数据库脚本 更新数据库中的 t_sys_params表】
        if (sysParams == null || StringUtils.isEmpty(sysParams.getParamsValue())) {
            throw new RuntimeException("请先到后台配置参数");
        }
        String paramsValue = sysParams.getParamsValue();
        // 此处可以存入redis
        return paramsValue;
    }

    @Override
    public SysParams getSysParamsByKey(String paramsKey) {
        QueryWrapper<SysParams> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("params_key", paramsKey);
        queryWrapper.eq("status", 1);
        queryWrapper.last("limit 1");
        SysParams sysParams = this.getOne(queryWrapper);
        return sysParams;
    }
}
